﻿using System.Collections.Generic;
using Microsoft.Xna.Framework.Input;

namespace Main.lib.input
{

    /*
     * Key
     * 
     * @brief   Handles the keys' states
     * @version 2011.11.14
     * 
     * @author  Bruno Miguel de Oliveira Tamer <bmotamer@gmail.com>
     */
    public sealed class Input
    {

     // Scope                   Type  Name          Value
        private static          int[] State       = new int[256];  //!< Container for all the keys' states
        private static readonly int   KEY_PRESS   = 0x01;          //!< Flag for pressed state
        private static readonly int   KEY_TRIGGER = 0x02;          //!< Flag for triggered state
        private static readonly int   KEY_RELEASE = 0x04;          //!< Flag for released state

        public static void Update()
        {
            KeyboardState KState = Keyboard.GetState();
            for (int Key = 0; Key < 256; ++Key)
            {
                int v = State[Key];
                if (KState.IsKeyDown((Keys)Key))
                    if ((v & KEY_PRESS) == 0)
                        v = KEY_TRIGGER | KEY_PRESS;
                    else
                        v = KEY_PRESS;
                else if ((v & KEY_PRESS) != 0)
                    v = KEY_RELEASE;
                else
                    v = 0;
                State[Key] = v;
            }
        }

        public static bool Trigger(Keys Key)
        {
            return (State[(int)Key] & KEY_TRIGGER) != 0;
        }

        public static bool Press(Keys Key)
        {
            return (State[(int)Key] & KEY_PRESS) != 0;
        }

        public static bool Release(Keys Key)
        {
            return (State[(int)Key] & KEY_RELEASE) != 0;
        }

    }

}